﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AssetManagementSystem.Model;
using AssetManagementSystem.Utils.Events;
using AssetManagementSystem.View.IView;
using AssetManagementSystem.Utils;

namespace AssetManagementSystem.Presenter {
    class HoldPresenter {

        HoldManager manager;
        IHoldView view;

        public HoldPresenter(IHoldView view) {
            manager = new HoldManager();
            this.view = view;
            //Handle Hold View Events
            this.view.Holds_LoadAll += new EventHandler(PresentAllHolds);
            this.view.Holds_Find += new EventHandler<SearchEventArgs>(PresentFoundHolds);
            this.view.Hold_Create += new EventHandler<HoldEventArgs>(CreateHold);
            this.view.Hold_Delete += new EventHandler<HoldEventArgs>(DeleteHold);
        }

        /// <summary>
        /// Provides a list of all holds to the view for display
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event args</param>
        private void PresentAllHolds(object sender, EventArgs e) {
            view.SetHoldList(manager.GetAllHolds());
        }

        /// <summary>
        /// Provides a list of holds found based on search data provided in event args
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event args, must contain search input</param>
        private void PresentFoundHolds(object sender, SearchEventArgs e) {
            view.SetHoldList(manager.FindHolds(e.SearchText));
        }

        /// <summary>
        /// Tries to create a new hold and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void CreateHold(object sender, HoldEventArgs e) {
            try {
                //store the new hold
                manager.CreateHold(e.Hold);
                //refresh the holds on the view
                PresentAllHolds(this, null);
                view.ShowMessage("Hold successfully saved.");
            } catch (Exception createFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(createFailed.Message);
            }
        }

        /// <summary>
        /// Tries to delete an existing hold and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void DeleteHold(object sender, HoldEventArgs e) {
            try {
                //delete the hold
                manager.DeleteHold(e.Hold);
                //refresh the holds on the view
                PresentAllHolds(this, null);
                view.ShowMessage("Hold successfully deleted.");
            } catch (Exception deleteFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(deleteFailed.Message);
            }
        }

    }
}
